<!DOCTYPE html>
<html>
<head>
    <th:block th:include="include :: header('连接记录')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">基类编码</label>
                <div class="layui-input-inline">
                    <input type="text" name="code"  autocomplete="off" lay-affix="clear" placeholder="基类编码" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="base-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="base-table" lay-filter="base-table"></table>
    </div>
</div>

<script type="text/html" id="base-toolbar">
    <button class="pear-btn pear-btn-md pear-btn-primary" lay-event="add-base">
        <i class="layui-icon pear-icon pear-icon-add"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="base-bar">
    <a class="pear-btn pear-btn-warming pear-btn-xs" lay-event="edit">编辑</a>
    <a class="pear-btn pear-btn-danger pear-btn-xs" lay-event="remove">删除</a>
</script>

<script type="text/html" id="base-status">
    <input type="checkbox" name="enable" value="{{d.userId}}" lay-skin="switch" lay-text="解锁|锁定"
           lay-filter="user-status" {{ d.status== 1 ? 'checked' : '' }} />
</script>

<script type="text/html" id="base-enable">
    <input type="checkbox" name="enable" value="{{d.userId}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="user-enable" {{ d.enable== 1 ? 'checked' : '' }} />
</script>

<script type="text/html" id="base-sex">
    {{#if (d.sex == 1) { }}
    <span>男</span>
    {{# }else if(d.sex == 2){ }}
    <span>女</span>
    {{# } }}
</script>

<script type="text/html" id="base-login">
    {{#if (d.login == 0) { }}
    <span>在线</span>
    {{# }else if(d.sex == 1){ }}
    <span>离线</span>
    {{# } }}
</script>

<script type="text/html" id="base-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd hh:MM:ss')}}
</script>

<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery','http'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let http = layui.http;

        let MODULE_PATH = "/baseEntity/";

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '基类编码',
                    field: 'code',
                    align: 'center',
                    width: 120
                },
                {
                    title: '基类包名',
                    field: 'packageName',
                    align: 'center'
                },
                {
                    title: '基类字段',
                    field: 'fields',
                    align: 'center'
                },
                {
                    title: '备注',
                    field: 'remark',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#base-bar',
                    align: 'center',
                    width: 180
                }
            ]
        ]
        const tableId = "base-table"
        table.render({
            elem: '#base-table',
            url: MODULE_PATH + "page",
            page: true,
            cols: cols,
            method:'post',
            skin: 'line',
            toolbar: '#base-toolbar',
            text: {none: '暂无信息'},
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(base-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(base-table)', function (obj) {
            if(obj.event === 'add-base'){
                window.import()
            }
            else if (obj.event === 'refresh') {
                window.refresh();
            }
            else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(base-query)', function (data) {
            table.reload(tableId, {
                where: data.field,
            })
            return false;
        });


        form.on('switch(base-status)', function (obj) {
            let status = this.checked ? 1 : 0
            let param = {userId:this.value,status:status}
        });

        form.on('switch(base-enable)', function () {
            let enable = this.checked ? 1 :0
            let param = {userId:this.value,enable:enable}
        });

        window.import = function (){
            layer.open({
                type: 2,
                title: '新增基类',
                shade: 0.1,
                area: ['550px', '480px'],
                content: MODULE_PATH + 'baseEntity-add.html'
            })
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '编辑基类',
                shade: 0.1,
                area: ['550px', '480px'],
                content: MODULE_PATH +'baseEntity-edit?id='+obj.data["id"]
            });
        }
        window.remove = function (obj) {
            layer.confirm('确定要删除该信息', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let param = [obj.data['id']]
                http.request({
                    url: MODULE_PATH + "delete",
                    contentType: http.contentType.json,
                    data:param,
                    method:"delete"
                }).then(res=>{
                    layer.msg(res.message, {icon: 1,time: 1000}, function () {
                        table.reload(tableId);
                    });
                })
            });
        }

        window.batchRemove = function (obj) {
            let checkIds = table.checkStatus(obj.config.id).data;
            if (checkIds.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = [];
            for(let i = 0;i<checkIds.length;i++){
                ids.push(checkIds[i].id)
            }
            layer.confirm('确定要删除这些数据', {
                icon: 3,
                title: '提示'
            }, function (index) {
                http.request({
                    url: MODULE_PATH + "delete",
                    contentType: http.contentType.json,
                    data:ids,
                    method:"delete"
                }).then(res=>{
                    layer.close(index);
                    layer.msg(res.message, {icon: 1,time: 1000}, function () {
                        table.reload(tableId);
                    });
                })
            });
        }
        window.refresh = function (param) {
            table.reload(tableId);
        }
    })
</script>
</body>
</html>
